from django.db import models

from apps.users.models import BaseModel
from apps.organiztion.models import Teacher, CourseOrg


class Course(BaseModel):
    course_org = models.ForeignKey(CourseOrg, null=True, blank=True, on_delete=models.CASCADE,
                                   verbose_name="所属课程机构")  # 外键，显示该课程属于哪个课程机构
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, verbose_name="讲师")
    name = models.CharField(verbose_name='课程名称', max_length=50)
    desc = models.CharField(verbose_name='课程描述', max_length=300)
    learn_times = models.IntegerField(default=0, verbose_name='课程时长(分钟数)')
    degree = models.CharField(verbose_name='课程难度', choices=(('cj', '初级'), ('zj', '中级'), ('gj', '高级')), max_length=2)
    students = models.IntegerField(default=0, verbose_name='学习人数')
    fav_nums = models.IntegerField(default=0, verbose_name='收藏人数')
    click_nums = models.IntegerField(default=0, verbose_name='点击数')
    category = models.CharField(default=u'后端开发', verbose_name='课程类别', max_length=20)
    tag = models.CharField(default='', verbose_name='课程标签', max_length=10)
    youneed_know = models.CharField(default='', verbose_name='课程须知', max_length=300)
    teacher_tell = models.CharField(default='', verbose_name='老师告诉你', max_length=300)

    is_classics = models.BooleanField(default=False, verbose_name="是否经典")
    detail = models.TextField(verbose_name='课程详情')  # 富文本信息
    image = models.ImageField(upload_to='courses/%Y/%m', verbose_name='封面图', max_length=100)

    class Meta:
        verbose_name = '课程信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Lesson(BaseModel):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, null=True,
                               blank=True, verbose_name='课程')  # on_delete 表示对应的外键数据被删除后，当前数据应该怎么办
    name = models.CharField(max_length=100, verbose_name=u'章节名')
    learn_times = models.IntegerField(default=0, verbose_name=u'学习时长(分钟数)')

    class Meta:
        verbose_name = '课程章节'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Video(BaseModel):
    lesson = models.ForeignKey(Lesson, verbose_name='章节', on_delete=models.CASCADE)
    name = models.CharField(max_length=100, verbose_name=u'视频名称')
    learn_times = models.IntegerField(default=0, verbose_name=u'学习时长(分钟数)')
    url = models.CharField(max_length=200, verbose_name=u'访问地址')

    class Meta:
        verbose_name = '章节视频'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class CourseResource(BaseModel):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, null=True,
                               blank=True, verbose_name='课程')  # on_delete 表示对应的外键数据被删除后，当前数据应该怎么办
    name = models.CharField(max_length=100, verbose_name=u'资源名称')
    file = models.FileField(upload_to='course/resource/%Y/%m', verbose_name='下载路径', max_length=200)

    class Meta:
        verbose_name = '课程资源'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
